"""
 -*- coding: utf-8-*-
 @File: base_config.py
 @Author: chen
 @time: 2023/09/10/ 17:26
"""
import time

import allure
import yaml
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from hogwartsHomework.homework_web_L4.tools.record_exception import record_exception
from hogwartsHomework.homework_web_L4.tools.record_log import logger


class PageBase:
    _BASE_URL = ""

    def __init__(self, base_driver=None):
        if base_driver:
            self.driver = base_driver
        else:
            self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)
        self.action = ActionChains(self.driver)
        # if not self.driver.current_url.startswith("http"):
        #     self.driver.get(self._BASE_URL)

    @record_exception
    def do_get(self, url):
        self.driver.get(url)
        logger.info(f"打开测试网站：{url}--->成功")

    @record_exception
    def do_find(self, locator: tuple):
        """获取单个元素"""
        element = self.driver.find_element(*locator)
        logger.info(f"查找元素：{locator}--->成功")
        return element

    @record_exception
    def do_finds(self, locator: tuple):
        elements = self.driver.find_elements(*locator)
        logger.info(f"查找元素：{locator}--->成功")
        return elements

    def execute_js(self, statement: str):
        self.driver.execute_script(statement)
        logger.info("执行js语句成功")

    @record_exception
    def do_click(self, locator: tuple):
        """点击元素"""
        self.do_find(locator).click()
        logger.info(f"点击元素：{locator}--->成功")

    @record_exception
    def do_input(self, locator: tuple, content: str):
        """输入内容"""
        self.do_find(locator).send_keys(content)
        logger.info(f"输入内容：{content}--->成功")

    @record_exception
    def do_quit(self):
        """退出浏览器进程"""
        self.driver.quit()
        logger.info("退出浏览器")

    @record_exception
    def wait_until_visibility(self, locator: tuple):
        """显示等待元素可以被定位"""
        WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located(locator))
        logger.info(f"等待元素：{locator}可定位--->成功")

    @record_exception
    def wait_until_clickable(self, locator: tuple):
        """显示等待元素可以被点击"""
        WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable(locator)
        )
        logger.info(f"等待元素：{locator}可点击--->成功")

    @record_exception
    def get_cookies(self):
        """将cookie写入文件"""
        # self.driver.get('https://work.weixin.qq.com/wework_admin/frame')
        time.sleep(20)
        with open("../cookies.yaml", "w") as f:
            yaml.safe_dump(self.driver.get_cookies(), f)
            logger.info("将cookie写入文件--->成功")

    @record_exception
    def add_cookies(self):
        """植入cookies"""
        for i in yaml.safe_load(open("../cookies.yaml")):
            self.driver.add_cookie(i)
        time.sleep(2)
        self.driver.refresh()
        time.sleep(2)
        logger.info("植入cookie--->成功")

    # def save(self):
    #     a = self.driver.page_source
    #     self.driver.save_screenshot()
